<?php

class M_kontak extends CI_Model {

	function __construct(){
		parent::__construct();
	}

	function kontak_get_count_rows($query){
		$sql="SELECT * FROM kontak";
		$num_rows = $this->m_public_function->get_count_rows($sql);
		return $num_rows;
	}

	function kontak_listview($query, $start, $end){

		$sql="SELECT *	FROM kontak";

		$num_rows = $this->m_public_function->get_count_rows($sql);
		$sql.=" LIMIT $start, $end";
		//$this->firephp->log($sql);

		$query = $this->db->query($sql);
		if($query->num_rows()>0){
			return $query->result();
		}else{
			return null;
		}

	}

	function getDetail($id){
		$this->db->where('id', $id);
		$query = $this->db->get('kontak');

		$num_rows = $query->num_rows();
		if($num_rows > 0){
			return $query->row();
		}
	}

	function kontak_list($query, $start, $end){

		$sql="SELECT *	 FROM kontak";
		$num_rows = $this->m_public_function->get_count_rows($sql);
		if($start != 0 && $end != 0){
			$sql.=" LIMIT $start, $end";
		}
		$query = $this->db->query($sql);

		if($num_rows > 0){
			$result = $query->result();
			$i = 0;
			foreach($result as $row){
				$data[$i] = $row;
				$i++;
			}
		}
		$final[] = array(
			"TotalRows"=>$num_rows,
			"results"=>$data
		);
		return json_encode($final);
	}


	function kontak_save($params){

		extract($params);
		if($task == 'create'){
			$this->db->insert('kontak',$data);
		}else{
			$this->db->where('id', $id);
			$this->db->update('kontak',$data);
		}

		if($this->db->affected_rows()){
			return 1;
		}else{
			return 0;
		}
	}

}